#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<math.h>
#include<Windows.h>
#include<stdlib.h>
#include<time.h>
#include<assert.h>
#include<stdbool.h>


typedef struct {
	int* data;
	int top;
	int tail;
	int size;
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k) {
	MyCircularQueue* a = NULL;
	a = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
	a->data = (int*)malloc(sizeof(int) * (k + 1));
	if (a == NULL)
	{
		assert("perror malloc");
	}
	else
	{
		a->tail = 0;
		a->top = 0;
		a->size = k + 1;
	}
	return a;
}
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
	if ((obj->tail + 1) % obj->size == obj->top)
	{
		return false;
	}
	obj->data[obj->tail] = value;
	obj->tail = (obj->tail + 1) % obj->size;
	return true;
}
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
	if (obj->tail == obj->top)
	{
		return false;
	}
	else
	{
		obj->top = (obj->top + 1) % obj->size;
		return true;
	}
}
int myCircularQueueFront(MyCircularQueue* obj) {
	if (obj->tail == obj->top)
	{
		return -1;
	}
	else
		return obj->data[obj->top];
}
int myCircularQueueRear(MyCircularQueue* obj) {
	if (obj->tail == obj->top)
	{
		return -1;
	}
	else
		return obj->data[(obj->tail - 1 + obj->size) % obj->size];
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
	if (obj->tail == obj->top)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool myCircularQueueIsFull(MyCircularQueue* obj) {
	if ((obj->tail + 1) % obj->size == obj->top)
	{
		return true;
	}
	else
	{
		return false;
	}
}
void myCircularQueueFree(MyCircularQueue* obj) {
	free(obj->data);
	free(obj);
}

int main()
{
	MyCircularQueue* a = NULL;
	a = myCircularQueueCreate(3);
	myCircularQueueEnQueue(a, 1);
	myCircularQueueEnQueue(a, 2);
	myCircularQueueEnQueue(a, 3);
	myCircularQueueEnQueue(a, 4);
+	myCircularQueueRear(a);
	myCircularQueueIsFull(a);
	myCircularQueueDeQueue(a);
	myCircularQueueEnQueue(a, 4);
	myCircularQueueRear(a);
}